package org.tloss.games.mummy;

import android.content.Intent;
import android.os.Bundle;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.example.games.basegameutils.GameHelper;
import com.google.example.games.basegameutils.GameHelper.GameHelperListener;

public class MainActivity extends AndroidApplication implements
		GameHelperListener {
	private GameHelper gameHelper;

	public MainActivity() {
		gameHelper = new GameHelper(this);
		gameHelper.enableDebugLog(true, "GPGS");
		if (!getSignedInGPGS()) {
			logoutGPGS();
		}
	}

	public boolean getSignedInGPGS() {
		return gameHelper.isSignedIn();
	}

	public void loginGPGS() {
		try {
			runOnUiThread(new Runnable() {
				public void run() {
					gameHelper.beginUserInitiatedSignIn();
				}
			});
		} catch (final Exception ex) {
		}
	}

	public void logoutGPGS() {
		gameHelper.signOut();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
		cfg.useGL20 = false;
		initialize(new MummyGame(), cfg);
		gameHelper.setup(this);

	}

	public void onStart() {
		super.onStart();
		gameHelper.onStart(this);
	}

	public void onStop() {
		super.onStop();
		gameHelper.onStop();
	}

	public void onActivityResult(int request, int response, Intent data) {
		super.onActivityResult(request, response, data);
		gameHelper.onActivityResult(request, response, data);
	}

	public void onSignInFailed() {

	}

	public void onSignInSucceeded() {

	}

	public void submitScoreGPGS(int score) {
		gameHelper.getGamesClient().submitScore("CgkI6574wJUXEAIQBw", score);
	}

	public void unlockAchievementGPGS(String achievementId) {
		gameHelper.getGamesClient().unlockAchievement(achievementId);
	}

	public void getLeaderboardGPGS() {
		startActivityForResult(gameHelper.getGamesClient()
				.getLeaderboardIntent("CgkI6574wJUXEAIQBw"), 100);
	}

	public void getAchievementsGPGS() {
		startActivityForResult(gameHelper.getGamesClient()
				.getAchievementsIntent(), 101);
	}
}